package com.itheima.book.manager.dao;

import com.itheima.book.manager.domain.Book;

import java.util.ArrayList;

public class BookDao2 implements BaseBookDao {
    //    Book[] books = new Book[5];
//    private  static Book[] books = new Book[5];
    private static ArrayList<Book> books = new ArrayList<Book>();

    public boolean addBook(Book book) {
//        Book[] books = new Book[5];
        int index = -1;
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i) == null) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            books.set(index, book);
            return true;
        }
        return false;
    }

    public Book[] getAllBooks() {

        Book[] books2 = new Book[books.size()];
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i) != null){
                books2[i] = books.get(i);
            }
        }
        return books2;
    }

    public boolean deleteBookByBookId(String bookId) {
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i) != null) {
                if (books.get(i).getBookId().equals(bookId)) {
                    books.remove(i);
                    return true;
                }
            }
        }
        return false;
    }

    public void updateBookByBookId(Book book) {
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i) != null) {
                if (books.get(i).getBookId().equals(book.getBookId())) {
                    books.set(i, book);
                }
            }
        }
    }
}
